import numpy as np
import matplotlib.pyplot as plt
import random
import threading

class Singleton:
    _instance = None
    _instance_lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            with cls._instance_lock:
                if not cls._instance:
                    cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

    def createMap(self, size, num_walls, min_wall_size = 1, max_wall_size = 1):
        self.size = size
        self.map_array = np.zeros((size, size), dtype=int)

        for _ in range(num_walls):
            # 生成随机的墙的位置和大小
            x1, y1 = random.randint(0, size - max_wall_size), random.randint(0, size - max_wall_size)
            x2, y2 = x1 + random.randint(min_wall_size, max_wall_size), y1 + random.randint(min_wall_size,
                                                                                            max_wall_size)
            # 确保墙不会超出地图边界
            x2 = min(x2, size - 1)
            y2 = min(y2, size - 1)

            # 在地图上绘制墙  1是墙
            self.map_array[x1:x2 + 1, y1:y2 + 1] = 1

            # 显示地图图片
        plt.imshow(self.map_array, cmap='gray', interpolation='nearest')
        plt.axis('on')  # 不显示坐标轴
        plt.show()
        return self.map_array

    def checkTerrain(self,x,y):
        map = Singleton()
        type = map.map_array[x,y]
        return type

# 单例入口
Map = Singleton()
MapTerrain = Map.createMap(100,99,1,10)


